﻿using System;
using UnityEngine;
using UnityEngine.UI;
using WssEvents;

namespace JQMergeFramework
{
    
    public class UsePropItemMask: MonoBehaviour
    {
        
        public Text tipsTxt;
        
        public RectTransform TopClickTrans,BottomClickTrans;

        public UISwitchChild propIconSW;
        
        private int residueNum = 0;

        private bool isForbidBlack = false;
        
        
        private UsePropTypes mSelectPropTypes = UsePropTypes.None;

        public UsePropTypes UsePropTypes
        {
            get { return mSelectPropTypes; }
        }

        private void Awake()
        {
            Vector3 size =this.GetComponent<RectTransform>().rect.size;
           
            float targetHight =size.y/ 1920.0f;
            
            if (targetHight>1)
            {
                TopClickTrans.sizeDelta = new Vector2(size.x, (size.y/2)-399);
                BottomClickTrans.sizeDelta = new Vector2(size.x, MathF.Abs((-size.y/2)+647));
            }
            else
            {
                TopClickTrans.sizeDelta = new Vector2(size.x, 561);
                BottomClickTrans.sizeDelta = new Vector2(size.x, 300);
            }
            
            UIManager.Instance.AddBtnListener(this.transform);
        }

        private void OnDisable()
        {
            GlobalEvent.Removelistener(GameEvents.OnPropUseResidueNum,OnPropUseResidueNum);
        }

        private void OnEnable()
        {
            GlobalEvent.Addlistener(GameEvents.OnPropUseResidueNum,OnPropUseResidueNum);
        }


        void OnPropUseResidueNum()
        {
            DataManager.Instance.SetPropNum(PropId.RemoveProp,-1);
            isForbidBlack = true;
            residueNum--;
            if (residueNum <= 0)
            {
                Close();
            }
        }

        public void Open(UsePropTypes types)
        {
            mSelectPropTypes = types;
            
            isForbidBlack = false;
            if (types == UsePropTypes.Remove)
            {
                tipsTxt.text = "请点击选中要消除的房子！";
                residueNum = GameCfg.RemoveCityLimitNum;
                //标记是否在使用移动道具
                MergeManager.Instance.isUseRemoveProp = true;
                propIconSW.index = 1;
            }
            else if (types == UsePropTypes.Move)
            {
                tipsTxt.text = "请点击选中要移动的区域！";
                //标记是否在使用移动道具
                MergeManager.Instance.isUseMoveProp = true;
                propIconSW.index = 0;
            }
        }

        public void Close()
        {
            if (mSelectPropTypes == UsePropTypes.Move)
            {
                MergeManager.Instance.CancelMoveCity();
            }
            
            MergeManager.Instance.isUseRemoveProp = false;
            MergeManager.Instance.isUseMoveProp = false;
            this.gameObject.SetActive(false);
        }

        
        void OnBtnClick(Transform trans)
        {
            AudioManager.Instance.Play(AudioName.Click);
            switch (trans.name)
            {
                case "BtnClose":
                    //丢弃第一个建筑
                    Close();
                    break;
            }
        }
        
    }
    
}